﻿$(function () {

    var rechargeModal = new abp.ModalManager(abp.appPath + 'Machines/RechargeModal');
    var rechargesModal = new abp.ModalManager(abp.appPath + 'Machines/RechargesModal');

    var dataTable = $('#MachinesTable').DataTable(abp.libs.datatables.normalizeConfiguration({
        ajax: abp.libs.datatables.createAjax(rechargeable.machines.machine.getList, inputAction),
        processing: true,
        serverSide: true,
        paging: true,
        searching: false,
        autoWidth: false,
        scrollCollapse: true,
        order: [[1, "asc"]],
        columnDefs: [
            {
                rowAction: {
                    items:
                        [
                            {
                                text: "删除",
                                action: function (data) {
                                    abp.message.confirm("确定要删除吗？", function () {
                                        rechargeable.machines.machine.delete(data.record.id,{
                                            success: function (data) {
                                                abp.message.success("删除成功");
                                                dataTable.ajax.reload();
                                            }
                                        });
                          
                                    });
                                }
                            },
                            {
                                text: "停用",
                                action: function (data) {
                                    if (data.record.autoRecharge === false) {
                                        abp.message.success("该机器是停用状态中");
                                        return;
                                    }
                                    abp.message.confirm("确定要停用吗？", function () {
                                        rechargeable.machines.machine.relieve(data.record.id, {
                                            success: function (data) {
                                                if (data === "00000000-0000-0000-0000-000000000000") {
                                                    abp.message.success("", "停用成功，因时间已经到期，未生成新的序列号");
                                                }
                                                else {
                                                    abp.message.success(data, "停用成更，新的序列号");
                                                }
                                                dataTable.ajax.reload();
                                            }
                                        });

                                    });
                                }
                            },
                            {
                                text: "启用",
                                action: function (data) {
                                    if (data.record.autoRecharge) {
                                        abp.message.success("该机器是启用状态中");
                                        return;
                                    }
                                    abp.message.confirm("确定要启用该机器吗？", function () {
                                        rechargeable.machines.machine.autoRecharge(data.record.id, {
                                            success: function (data) {
                                                dataTable.ajax.reload();
                                            }
                                        });

                                    });
                                }
                            }

                        ]
                }

            },
            {data:"id"},
            { data: "name" },
            { data: "driveId" },
            { data: "desc" },
            {
                data: "autoRecharge"

            },
            { data: "lastOnlineTime" },
            { data: "expireTime" }
        ]
    }));

    $("#btnReachines").on("click", function () {
        rechargesModal.open();
        var t = setInterval(function () {
            var $form = rechargesModal.getForm();
            if ($form !== null) {
                $form.on('abp-ajax-success',
                    function (event, data) {
                        abp.message.info(data.responseText, "操作提示");
                    });
                clearInterval(t);
            }

        }, 100);
    });
    $("#btnSearch").on("click", function () {
        console.log("搜索");
        dataTable.ajax.reload();
    });
    rechargeModal.onResult(function () {
        dataTable.ajax.reload();
    });
});

function inputAction() {
    return {
        productId: $("#Products").val(),
        key: $("#key").val()
    };
}